{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "f77d6a66",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "b'Skipping line 1513591: expected 23 fields, saw 24\\n'\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(19235, 23)"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#导入相关库\n",
    "import warnings\n",
    "warnings.filterwarnings( \"ignore\" )\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import time\n",
    "import seaborn as sns\n",
    "import matplotlib.pyplot as plt\n",
    "from subprocess import check_output\n",
    "import lightgbm as lgb\n",
    "import matplotlib.pyplot as plt\n",
    "from sklearn.ensemble import GradientBoostingClassifier\n",
    "from tqdm import tqdm_notebook as tqdm\n",
    "from sklearn.preprocessing import MinMaxScaler\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.metrics import confusion_matrix, log_loss\n",
    "%matplotlib inline\n",
    "#读取教据\n",
    "df = pd.read_csv('./data/Chicago_Crimes.csv',error_bad_lines=False)\n",
    "#并随机抽取，抽取数据集的1%\n",
    "df_sample = df.sample(frac=0.01)\n",
    "df_sample.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "c0a3bc46",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Unnamed: 0</th>\n",
       "      <th>ID</th>\n",
       "      <th>Case Number</th>\n",
       "      <th>Date</th>\n",
       "      <th>Block</th>\n",
       "      <th>IUCR</th>\n",
       "      <th>Primary Type</th>\n",
       "      <th>Description</th>\n",
       "      <th>Location Description</th>\n",
       "      <th>Arrest</th>\n",
       "      <th>...</th>\n",
       "      <th>Ward</th>\n",
       "      <th>Community Area</th>\n",
       "      <th>FBI Code</th>\n",
       "      <th>X Coordinate</th>\n",
       "      <th>Y Coordinate</th>\n",
       "      <th>Year</th>\n",
       "      <th>Updated On</th>\n",
       "      <th>Latitude</th>\n",
       "      <th>Longitude</th>\n",
       "      <th>Location</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1143643</th>\n",
       "      <td>4747283</td>\n",
       "      <td>2745567</td>\n",
       "      <td>HJ380231</td>\n",
       "      <td>05/21/2003 11:00:00 PM</td>\n",
       "      <td>015XX S MILLARD AVE</td>\n",
       "      <td>0810</td>\n",
       "      <td>THEFT</td>\n",
       "      <td>OVER $500</td>\n",
       "      <td>RESIDENCE</td>\n",
       "      <td>False</td>\n",
       "      <td>...</td>\n",
       "      <td>24.0</td>\n",
       "      <td>29.0</td>\n",
       "      <td>06</td>\n",
       "      <td>1152294.0</td>\n",
       "      <td>1892155.0</td>\n",
       "      <td>2003.0</td>\n",
       "      <td>04/15/2016 08:55:02 AM</td>\n",
       "      <td>41.859938</td>\n",
       "      <td>-87.716451</td>\n",
       "      <td>(41.859937564, -87.716451145)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1275322</th>\n",
       "      <td>4879100</td>\n",
       "      <td>2913298</td>\n",
       "      <td>HJ589956</td>\n",
       "      <td>08/27/2003 08:39:37 AM</td>\n",
       "      <td>107XX S DR MARTIN LUTHER KING JR DR</td>\n",
       "      <td>0610</td>\n",
       "      <td>BURGLARY</td>\n",
       "      <td>FORCIBLE ENTRY</td>\n",
       "      <td>OTHER</td>\n",
       "      <td>False</td>\n",
       "      <td>...</td>\n",
       "      <td>9.0</td>\n",
       "      <td>49.0</td>\n",
       "      <td>05</td>\n",
       "      <td>1180817.0</td>\n",
       "      <td>1833749.0</td>\n",
       "      <td>2003.0</td>\n",
       "      <td>04/15/2016 08:55:02 AM</td>\n",
       "      <td>41.699056</td>\n",
       "      <td>-87.613546</td>\n",
       "      <td>(41.699055764, -87.613546407)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1729398</th>\n",
       "      <td>5329857</td>\n",
       "      <td>3479188</td>\n",
       "      <td>HK526730</td>\n",
       "      <td>07/31/2004 01:00:00 AM</td>\n",
       "      <td>036XX W AUGUSTA BLVD</td>\n",
       "      <td>502P</td>\n",
       "      <td>OTHER OFFENSE</td>\n",
       "      <td>FALSE/STOLEN/ALTERED TRP</td>\n",
       "      <td>STREET</td>\n",
       "      <td>True</td>\n",
       "      <td>...</td>\n",
       "      <td>27.0</td>\n",
       "      <td>23.0</td>\n",
       "      <td>26</td>\n",
       "      <td>1152113.0</td>\n",
       "      <td>1906427.0</td>\n",
       "      <td>2004.0</td>\n",
       "      <td>04/15/2016 08:55:02 AM</td>\n",
       "      <td>41.899105</td>\n",
       "      <td>-87.716739</td>\n",
       "      <td>(41.899105026, -87.716739169)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>648367</th>\n",
       "      <td>4250261</td>\n",
       "      <td>2128650</td>\n",
       "      <td>HH366656</td>\n",
       "      <td>05/12/2002 01:10:00 PM</td>\n",
       "      <td>106XX S SANGAMON ST</td>\n",
       "      <td>0460</td>\n",
       "      <td>BATTERY</td>\n",
       "      <td>SIMPLE</td>\n",
       "      <td>RESIDENCE PORCH/HALLWAY</td>\n",
       "      <td>False</td>\n",
       "      <td>...</td>\n",
       "      <td>34.0</td>\n",
       "      <td>73.0</td>\n",
       "      <td>08B</td>\n",
       "      <td>1171872.0</td>\n",
       "      <td>1834243.0</td>\n",
       "      <td>2002.0</td>\n",
       "      <td>04/15/2016 08:55:02 AM</td>\n",
       "      <td>41.700612</td>\n",
       "      <td>-87.646284</td>\n",
       "      <td>(41.700611852, -87.646284361)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>880844</th>\n",
       "      <td>4483692</td>\n",
       "      <td>2415248</td>\n",
       "      <td>HH731484</td>\n",
       "      <td>10/22/2002 05:55:14 PM</td>\n",
       "      <td>028XX S CHRISTIANA AVE</td>\n",
       "      <td>0486</td>\n",
       "      <td>BATTERY</td>\n",
       "      <td>DOMESTIC BATTERY SIMPLE</td>\n",
       "      <td>RESIDENCE</td>\n",
       "      <td>True</td>\n",
       "      <td>...</td>\n",
       "      <td>22.0</td>\n",
       "      <td>30.0</td>\n",
       "      <td>08B</td>\n",
       "      <td>1154494.0</td>\n",
       "      <td>1884922.0</td>\n",
       "      <td>2002.0</td>\n",
       "      <td>04/15/2016 08:55:02 AM</td>\n",
       "      <td>41.840046</td>\n",
       "      <td>-87.708569</td>\n",
       "      <td>(41.840045708, -87.708568666)</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 23 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "         Unnamed: 0       ID Case Number                    Date  \\\n",
       "1143643     4747283  2745567    HJ380231  05/21/2003 11:00:00 PM   \n",
       "1275322     4879100  2913298    HJ589956  08/27/2003 08:39:37 AM   \n",
       "1729398     5329857  3479188    HK526730  07/31/2004 01:00:00 AM   \n",
       "648367      4250261  2128650    HH366656  05/12/2002 01:10:00 PM   \n",
       "880844      4483692  2415248    HH731484  10/22/2002 05:55:14 PM   \n",
       "\n",
       "                                       Block  IUCR   Primary Type  \\\n",
       "1143643                  015XX S MILLARD AVE  0810          THEFT   \n",
       "1275322  107XX S DR MARTIN LUTHER KING JR DR  0610       BURGLARY   \n",
       "1729398                 036XX W AUGUSTA BLVD  502P  OTHER OFFENSE   \n",
       "648367                   106XX S SANGAMON ST  0460        BATTERY   \n",
       "880844                028XX S CHRISTIANA AVE  0486        BATTERY   \n",
       "\n",
       "                      Description     Location Description  Arrest  ...  Ward  \\\n",
       "1143643                 OVER $500                RESIDENCE   False  ...  24.0   \n",
       "1275322            FORCIBLE ENTRY                    OTHER   False  ...   9.0   \n",
       "1729398  FALSE/STOLEN/ALTERED TRP                   STREET    True  ...  27.0   \n",
       "648367                     SIMPLE  RESIDENCE PORCH/HALLWAY   False  ...  34.0   \n",
       "880844    DOMESTIC BATTERY SIMPLE                RESIDENCE    True  ...  22.0   \n",
       "\n",
       "         Community Area  FBI Code  X Coordinate  Y Coordinate    Year  \\\n",
       "1143643            29.0        06     1152294.0     1892155.0  2003.0   \n",
       "1275322            49.0        05     1180817.0     1833749.0  2003.0   \n",
       "1729398            23.0        26     1152113.0     1906427.0  2004.0   \n",
       "648367             73.0       08B     1171872.0     1834243.0  2002.0   \n",
       "880844             30.0       08B     1154494.0     1884922.0  2002.0   \n",
       "\n",
       "                     Updated On   Latitude  Longitude  \\\n",
       "1143643  04/15/2016 08:55:02 AM  41.859938 -87.716451   \n",
       "1275322  04/15/2016 08:55:02 AM  41.699056 -87.613546   \n",
       "1729398  04/15/2016 08:55:02 AM  41.899105 -87.716739   \n",
       "648367   04/15/2016 08:55:02 AM  41.700612 -87.646284   \n",
       "880844   04/15/2016 08:55:02 AM  41.840046 -87.708569   \n",
       "\n",
       "                              Location  \n",
       "1143643  (41.859937564, -87.716451145)  \n",
       "1275322  (41.699055764, -87.613546407)  \n",
       "1729398  (41.899105026, -87.716739169)  \n",
       "648367   (41.700611852, -87.646284361)  \n",
       "880844   (41.840045708, -87.708568666)  \n",
       "\n",
       "[5 rows x 23 columns]"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_sample.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "336feab4",
   "metadata": {},
   "outputs": [],
   "source": [
    "#其中IUCR，FBI Code , Case Number，IDI它们是一种主要类型本身的编码，会给我们一个不切实际的效果很好的预测，用del直接删除\n",
    "del df_sample['IUCR']\n",
    "del df_sample['Case Number']\n",
    "del df_sample['ID']\n",
    "del df_sample['FBI Code']\n",
    "del df_sample['Updated On']\n",
    "del df_sample['Arrest']\n",
    "del df_sample['Domestic']\n",
    "del df_sample['Unnamed: 0']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "424228b8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(19235, 15)"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_sample.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "c427fc7f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Date                    0\n",
       "Block                   0\n",
       "Primary Type            0\n",
       "Description             0\n",
       "Location Description    0\n",
       "Beat                    0\n",
       "District                0\n",
       "Ward                    0\n",
       "Community Area          0\n",
       "X Coordinate            0\n",
       "Y Coordinate            0\n",
       "Year                    0\n",
       "Latitude                0\n",
       "Longitude               0\n",
       "Location                0\n",
       "dtype: int64"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#使用describe函数查看数值型数据和标量型数据的基本信息，包括最小值、最大值、均值、四分位数、总教等，查看数据的缺失值数量和占比情况\n",
    "df_sample.info\n",
    "df_sample.isnull().sum()\n",
    "df_sample.describe()\n",
    "df_sample.describe(include='O')\n",
    "df_na = pd.DataFrame(data=df_sample.isnull().sum()/df.shape[0],columns=['miss_rate']).sort_values(by='miss_rate',ascending=False)\n",
    "df_sample.dropna(inplace=True)\n",
    "df_sample.isnull().sum()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f74c93d2",
   "metadata": {},
   "source": [
    "1.先将字段Date转为datetime型，再扩展字段，提取年，月，周，日，小时信息。同时删除Date字段。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "dbae1a77",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 考生填写\n",
    "df_sample['Date'] = df_sample['Date'].astype(np.datetime64)\n",
    "#扩展字段，提取月，周，日，小时信息\n",
    "df_sample['year'] = df_sample['Date'].dt.year\n",
    "df_sample['month'] = df_sample['Date'].dt.month\n",
    "df_sample['weekday'] = df_sample['Date'].dt.weekday\n",
    "df_sample['day'] = df_sample['Date'].dt.day\n",
    "df_sample['hour'] = df_sample['Date'].dt.hour\n",
    "#删除Date\n",
    "del df_sample['Date']\n",
    "# 考生填写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "b2f53d5f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "Int64Index: 12133 entries, 1143643 to 1415584\n",
      "Data columns (total 19 columns):\n",
      " #   Column                Non-Null Count  Dtype  \n",
      "---  ------                --------------  -----  \n",
      " 0   Block                 12133 non-null  object \n",
      " 1   Primary Type          12133 non-null  object \n",
      " 2   Description           12133 non-null  object \n",
      " 3   Location Description  12133 non-null  object \n",
      " 4   Beat                  12133 non-null  int64  \n",
      " 5   District              12133 non-null  float64\n",
      " 6   Ward                  12133 non-null  float64\n",
      " 7   Community Area        12133 non-null  float64\n",
      " 8   X Coordinate          12133 non-null  float64\n",
      " 9   Y Coordinate          12133 non-null  object \n",
      " 10  Year                  12133 non-null  float64\n",
      " 11  Latitude              12133 non-null  object \n",
      " 12  Longitude             12133 non-null  float64\n",
      " 13  Location              12133 non-null  object \n",
      " 14  year                  12133 non-null  int64  \n",
      " 15  month                 12133 non-null  int64  \n",
      " 16  weekday               12133 non-null  int64  \n",
      " 17  day                   12133 non-null  int64  \n",
      " 18  hour                  12133 non-null  int64  \n",
      "dtypes: float64(6), int64(6), object(7)\n",
      "memory usage: 1.9+ MB\n"
     ]
    }
   ],
   "source": [
    "df_sample.info()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "50a97dd4",
   "metadata": {},
   "source": [
    "2.字符串类型字段\"BlocK,'Primary Type,'Description,Location Description,Location，在进行数据分析之前需要数值化，提高运行效率。factorize函数可以将字符串类型数据映射为一组数字，相同的字符串类型映射为相同的数字。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "cc05cab1",
   "metadata": {},
   "outputs": [],
   "source": [
    "#factorize 函数，这个函数可以方便地将离散特征转化为数值型，并且保留了原来的类别信息。\n",
    "#它会返回两个值:\n",
    "#编码数组:一个一维数组，表示每个类别对应的编码;\n",
    "#唯一类别数组:一个包含唯一类别的数组，顺序与编码数组一致。\n",
    "# 考生填写\n",
    "# 我认为此处要加Latitude\n",
    "col_list = ['Block','Primary Type','Description','Location Description','Location']\n",
    "\n",
    "for col in col_list:\n",
    "    df_sample[col] = pd.factorize(df_sample[col])[0]\n",
    "#把Primary Type名字改为Primary_Type\n",
    "df_sample['Primary_Type'] = df_sample['Primary Type']\n",
    "del df_sample['Primary Type']\n",
    "# 考生填写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "253a095a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "Int64Index: 12133 entries, 1143643 to 1415584\n",
      "Data columns (total 19 columns):\n",
      " #   Column                Non-Null Count  Dtype  \n",
      "---  ------                --------------  -----  \n",
      " 0   Block                 12133 non-null  int64  \n",
      " 1   Description           12133 non-null  int64  \n",
      " 2   Location Description  12133 non-null  int64  \n",
      " 3   Beat                  12133 non-null  int64  \n",
      " 4   District              12133 non-null  float64\n",
      " 5   Ward                  12133 non-null  float64\n",
      " 6   Community Area        12133 non-null  float64\n",
      " 7   X Coordinate          12133 non-null  float64\n",
      " 8   Y Coordinate          12133 non-null  object \n",
      " 9   Year                  12133 non-null  float64\n",
      " 10  Latitude              12133 non-null  object \n",
      " 11  Longitude             12133 non-null  float64\n",
      " 12  Location              12133 non-null  int64  \n",
      " 13  year                  12133 non-null  int64  \n",
      " 14  month                 12133 non-null  int64  \n",
      " 15  weekday               12133 non-null  int64  \n",
      " 16  day                   12133 non-null  int64  \n",
      " 17  hour                  12133 non-null  int64  \n",
      " 18  Primary_Type          12133 non-null  int64  \n",
      "dtypes: float64(6), int64(11), object(2)\n",
      "memory usage: 1.9+ MB\n"
     ]
    }
   ],
   "source": [
    "df_sample.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "842c48d7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "Int64Index: 8493 entries, 1763985 to 1813729\n",
      "Data columns (total 18 columns):\n",
      " #   Column                Non-Null Count  Dtype  \n",
      "---  ------                --------------  -----  \n",
      " 0   Block                 8493 non-null   int64  \n",
      " 1   Description           8493 non-null   int64  \n",
      " 2   Location Description  8493 non-null   int64  \n",
      " 3   Beat                  8493 non-null   int64  \n",
      " 4   District              8493 non-null   float64\n",
      " 5   Ward                  8493 non-null   float64\n",
      " 6   Community Area        8493 non-null   float64\n",
      " 7   X Coordinate          8493 non-null   float64\n",
      " 8   Y Coordinate          8493 non-null   float64\n",
      " 9   Year                  8493 non-null   float64\n",
      " 10  Latitude              8493 non-null   object \n",
      " 11  Longitude             8493 non-null   float64\n",
      " 12  Location              8493 non-null   int64  \n",
      " 13  year                  8493 non-null   int64  \n",
      " 14  month                 8493 non-null   int64  \n",
      " 15  weekday               8493 non-null   int64  \n",
      " 16  day                   8493 non-null   int64  \n",
      " 17  hour                  8493 non-null   int64  \n",
      "dtypes: float64(7), int64(10), object(1)\n",
      "memory usage: 1.2+ MB\n"
     ]
    }
   ],
   "source": [
    "# 数值类型数据处理\n",
    "#采用MinMaxScaler对数据进行规范化\n",
    "from sklearn.preprocessing import MinMaxScaler\n",
    "df_sample['X Coordinate']= df_sample['X Coordinate'].astype(float)\n",
    "df_sample['Y Coordinate']= df_sample['Y Coordinate'].astype(float)\n",
    "df_sample['X Coordinate'] = MinMaxScaler().fit_transform(df_sample['X Coordinate'].values.reshape(-1, 1))\n",
    "df_sample['Y Coordinate'] = MinMaxScaler().fit_transform(df_sample['Y Coordinate'].values.reshape(-1, 1))\n",
    "from sklearn.model_selection import train_test_split\n",
    "\n",
    "#拆分数据\n",
    "X_train, X_test, y_train, y_test = train_test_split(df_sample.loc[:, df_sample.columns !='Primary_Type'],\\\n",
    "df_sample['Primary_Type'],\\\n",
    "test_size=0.3,\\\n",
    "random_state=42)\n",
    "X_train.info()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ec030f5e",
   "metadata": {},
   "source": [
    "3.使用GradientBoostingClassifier分类器进行训练模型model_gbdt."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "5eba876e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "fl_score为0.843956043956044\n"
     ]
    }
   ],
   "source": [
    "from sklearn.ensemble import GradientBoostingClassifier\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "from sklearn.metrics import f1_score\n",
    "#考生完成\n",
    "model_gbdt = GradientBoostingClassifier(n_estimators=8)\n",
    "model_gbdt.fit(X_train,y_train)\n",
    "#考生完成\n",
    "y_prel = model_gbdt.predict(X_test)\n",
    "f1_scorel = f1_score(y_test, y_prel, average='micro')\n",
    "print('fl_score为{}'.format(f1_scorel))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b0672faf",
   "metadata": {},
   "source": [
    "4.使用网格搜索交叉验证对模型mode1_gbdt进行优化，调整参数learning_rate建议值为[0.1,0.2,0.3,0.4.0.5]，cv采用5折进行模型训练，得到最优模型。最优参数和最优评分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "4a5b6ae1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "nan"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#由考生填写\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "from sklearn.metrics import make_scorer\n",
    "param_grids = {'learning_rate':[0.1,0.2,0.3,0.4,0.5]}\n",
    "model_gs = GridSearchCV(estimator=model_gbdt,param_grid=param_grids,cv=5,scoring=make_scorer(f1_score))\n",
    "model_gs.fit(X_train,y_train)\n",
    "\n",
    "# 最优模型\n",
    "model_gs.best_estimator_\n",
    "#最优参数\n",
    "model_gs.best_params_\n",
    "#最优评分\n",
    "model_gs.best_score_\n",
    "#由考生填写"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9aafaae4",
   "metadata": {},
   "source": [
    "5.使用votingclassifier聚合了多个基础模型的预测结果。通过硬投票，软投票和自定义权重的软投票三种方式进行比较，确定最后的结果。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "8e6f5c0b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Accuracy: nan (+/- nan) [XGBoosting]\n",
      "Accuracy: 0.32 (+/- 0.00) [Random Forest]\n",
      "Accuracy: 0.21 (+/- 0.01) [SVC]\n",
      "Accuracy: nan (+/- nan) [voting]\n",
      "Accuracy: nan (+/- nan) [XGBoosting]\n",
      "Accuracy: 0.32 (+/- 0.00) [Random Forest]\n",
      "Accuracy: 0.21 (+/- 0.01) [SVC]\n",
      "Accuracy: nan (+/- nan) [voting]\n",
      "Accuracy: nan (+/- nan) [XGBoost]\n",
      "Accuracy: 0.32 (+/- 0.00) [Random Forest]\n",
      "Accuracy: 0.21 (+/- 0.01) [SVC]\n",
      "Accuracy: nan (+/- nan) [voting]\n"
     ]
    }
   ],
   "source": [
    "#由考生填写\n",
    "from sklearn.model_selection import cross_val_score\n",
    "from sklearn.linear_model import LinearRegression\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "from sklearn.svm import SVC\n",
    "from sklearn.ensemble import VotingClassifier\n",
    "from sklearn.ensemble import AdaBoostClassifier\n",
    "from xgboost import XGBClassifier\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "import warnings\n",
    "warnings.filterwarnings(\"ignore\")\n",
    "\n",
    "#clf1 = XGBClassifier(learning_rate=0.1,n_estimators=150,max_depth=3,min_child_weight=2,subsample=0.7,colsample_bytree=0.6,objective='binary:logistic')\n",
    "clf1 = XGBClassifier(learning_rate=0.1,n_estimators=150, max_depth=3, min_child_weight=2,subsample=0.7,colsample_bytree=0.6,objective= 'binary:logistic')\n",
    "clf2 = RandomForestClassifier(n_estimators=50,max_depth=1,min_samples_split=4,min_samples_leaf=63,oob_score=True)\n",
    "clf3 = SVC(C=0.1,probability=True)# 软投票的时候，probability必须指定为True\n",
    "clf = VotingClassifier(estimators=[('xgb',clf1),('rf',clf2),('svc',clf3)],voting='hard')\n",
    "# 硬投票\n",
    "eclf = VotingClassifier(estimators=[('xgb',clf1),('rf',clf2),('svc',clf3)],voting='hard')\n",
    "for clf,label in zip([clf1,clf2,clf3,eclf],['XGBoosting','Random Forest','SVC','voting']):\n",
    "    scores = cross_val_score(clf,X_train,y_train,cv=5,scoring='accuracy')\n",
    "    print(\"Accuracy: %0.2f (+/- %0.2f) [%s]\" % (scores.mean(),scores.std(),label))\n",
    "# 软投票voting=‘soft’\n",
    "eclf = VotingClassifier(estimators=[('xgb',clf1),('rf',clf2),('svc',clf3)],voting='soft')\n",
    "for clf,label in zip([clf1,clf2,clf3,eclf],['XGBoosting','Random Forest','SVC','voting']):\n",
    "    scores = cross_val_score(clf,X_train,y_train,cv=5,scoring='accuracy')\n",
    "    print(\"Accuracy: %0.2f (+/- %0.2f) [%s]\" % (scores.mean(),scores.std(),label))\n",
    "# 自定义投票\n",
    "eclf = VotingClassifier(estimators=[('xgb',clf1),('rf',clf2),('svc',clf3)],voting='soft',weights=[10,1,9])\n",
    "for clf,label in zip([clf1,clf2,clf3,eclf],['XGBoost','Random Forest','SVC','voting']):\n",
    "    scores = cross_val_score(clf,X_train,y_train,cv=5,scoring='accuracy')\n",
    "    print(\"Accuracy: %0.2f (+/- %0.2f) [%s]\" % (scores.mean(),scores.std(),label))\n",
    "#由考生填写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7aa66458",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
